#!/usr/bin/perl
#globalnet-lib.pl

do '../web-lib.pl';
&init_config();

sub get_gw2()
{
    local $gw2;
    open(GW2, "/sysvol/etc/sysconfig/gw2");
    my @aa=<GW2>;
    close(GW2);
    @bb=split(/ /, $aa[0]);
    $gw2 = $bb[4];
    return $gw2;
}

sub save_gw2()
{  
   $gw2_line = "route add default gw ";
   $gw2_line .= $in{'gw2'};
   open(GWT, ">/sysvol/etc/sysconfig/gw2");
   print GWT $gw2_line;
   close();
}

sub get_gateway_default()
{
	local($gateway, @lines, $l);
	open(IFC, "route -n |");
 	while(<IFC>) {
 		s/\r|\n//g;
 		if (/^\S+/) { push(@lines, $_); }
 		else { $lines[$#lines] .= $_; }
 	}
 	close(IFC);
 	foreach $l (@lines) {
 		if($l =~ /^0.0.0.0\s+(\S+)\s+0.0.0.0/)
 		{
 			$gateway = $1;
 		}
 	}
 	return $gateway;
}

sub get_dns_config
{
local $dns;
open(RESOLV, "/etc/resolv.conf");
while(<RESOLV>) {
	s/\r|\n//g;
	s/#.*$//;
	if (/nameserver\s+(.*)/) {
		push(@{$dns->{'nameserver'}}, split(/\s+/, $1));
		}
	elsif (/domain\s+(\S+)/) {
		$dns->{'domain'} = [ $1 ];
		}
	elsif (/search\s+(.*)/) {
		$dns->{'domain'} = [ split(/\s+/, $1) ];
		}
	}
close(RESOLV);

return $dns;
}

sub save_dns_config
{
&lock_file("/etc/resolv.conf");
open(RESOLV, "/etc/resolv.conf");
local @resolv = <RESOLV>;
close(RESOLV);
open(RESOLV, ">/etc/resolv.conf");
foreach (@{$_[0]->{'nameserver'}}) {
	print RESOLV "nameserver $_\n";
	}
if ($_[0]->{'domain'}) {
	if ($_[0]->{'domain'}->[1]) {
		print RESOLV "search ",join(" ", @{$_[0]->{'domain'}}),"\n";
		}
	else {
		print RESOLV "domain $_[0]->{'domain'}->[0]\n";
		}
	}
foreach (@resolv) {
	print RESOLV $_ if (!/^\s*(nameserver|domain|search)\s+/);
	}
close(RESOLV);
&unlock_file("/etc/resolv.conf");
}
#mdm_setdns(\@dnsip)
sub mdm_setdns()
{
	my $errcode = "Success";
	&lock_file("/etc/resolv.conf");
	open(RESOLV, "/etc/resolv.conf");
	local @resolv = <RESOLV>;
	close(RESOLV);
	open(RESOLV, ">/etc/resolv.conf");

	$dnslist = $_[0];
	foreach (@$dnslist){
		print RESOLV "nameserver $_\n";
	}
	foreach (@resolv) {
	        print RESOLV $_ if (!/^\s*(nameserver)\s+/);
        }
	close(RESOLV);
	&unlock_file("/etc/resolv.conf");
	return $errcode;
}
#mdm_gateway($IP)
sub mdm_gateway()
{
	my $errcode = "Success";
        &read_env_file($config{'network_config'}, \%conf);
        my $gatewaydev = ($conf{'GATEWAYDEV'})?$conf{'GATEWAYDEV'}:"eth0";
        my $gatewayfile = "/etc/sysconfig/network-scripts/ifcfg-".$gatewaydev;
        &lock_file($gatewayfile);
        &read_env_file($gatewayfile, \%confi);
        $confi{'GATEWAY'} = $_[0];
        &write_env_file($gatewayfile, \%confi);
        &unlock_file($gatewayfile);
        `route del default`;
	if($gatewaydev eq "bond0")
	{
               `ifup bond0`;
	}
	else
	{
               `ifup eth0`;
	}
	return $errcode;

}
sub list_hosts
{
local @rv;
local $lnum = 0;
open(HOSTS, $config{'hosts_file'});
while(<HOSTS>) {
	s/\r|\n//g;
	s/#.*$//g;
	s/\s+$//g;
	if (/(\d+\.\d+\.\d+\.\d+)\s+(.*)$/) {
		if ( $1 ne "127.0.0.1")
		{
			push(@rv, "$1=$2");
		}	

		}
	$lnum++;
	}
close(HOSTS);
return @rv;
}

sub save_hosts
{
local $localhost = undef;	
&lock_file($config{'hosts_file'});
open(HOSTS, $config{'hosts_file'});
while($line = <HOSTS>) {
	$line =~ s/\r|\n//g;
	$line =~ s/#.*$//g;
	$line =~ s/\s+$//g;
	if ($line =~ /(\d+\.\d+\.\d+\.\d+)\s+(.*)$/) {
		if ( $1 eq "127.0.0.1")
		{
			$localhost = $line;
		}	

		}
	}
close(HOSTS);
open(HOSTS, ">$config{'hosts_file'}");
print HOSTS $localhost, "\n" if($localhost);
foreach $d (@{$_[0]}) {
	$d =~ s/=/\t/;
	print HOSTS $d, "\n";
}
close(HOSTS);
&unlock_file($config{'hosts_file'});
}
1;
